package com.huanong.it.ui;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huanong.it.ui.entity.Link;
import com.huanong.it.ui.entity.User;
import com.huanong.it.ui.service.LinkService;
import com.huanong.it.ui.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@CrossOrigin(origins = "*")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private LinkService linkService;

    @RequestMapping("/reg")
    @ResponseBody
    public Map<String, Object> regUser(String name, String pwd) {
        User u = new User();
        u.setName(name);
        u.setPwd(pwd);
        u.setLinkTotal(0);
        userService.save(u);

        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "注册成功");
        return map;
    }

    @RequestMapping("/login")
    @ResponseBody
//    public Map<String, Object> login(String name, String pwd, HttpSession session){
    public Map<String, Object> login(String name, String pwd) {
        QueryWrapper<User> qw = new QueryWrapper<>();
//        qw.eq("user_name",name);
//        qw.eq("user_pwd",pwd);
        //下面这种方式无需去找数据库的字段名，比较方便
        qw.lambda().eq(User::getName, name);
        qw.lambda().eq(User::getPwd, pwd);

        User u = userService.getOne(qw);
        Map<String, Object> map = new HashMap<>();
        if (u != null) {
            map.put("code", 200);
            map.put("cur_user", u);
//            session.setAttribute("cur_user",u);
            return map;
        }
        map.put("code", 500);
        map.put("err", "用户名或密码错误");
        return map;
    }

    @RequestMapping("/list")
    @ResponseBody
    public Map<String, Object> list() {
        List<Link> linkList = linkService.list();
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "查询成功");
        map.put("data", linkList);
        return map;
    }

    @RequestMapping("/add")
    @ResponseBody
    public Map<String, Object> add(String name, String phone) {
        Link link = new Link();
        link.setName(name);
        link.setPhone(phone);
        linkService.save(link);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "添加成功");
        return map;
    }
    @RequestMapping("/delete")
    @ResponseBody
    public Map<String, Object> delete(int id) {
        linkService.removeById(id);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "删除成功");
        return map;
    }

    @RequestMapping("/getLinkInfoById")
    @ResponseBody
    public Map<String, Object> getLinkInfoById(int id) {
        Link link = linkService.getById(id);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "查询成功");
        map.put("data", link);
        return map;
    }

    @RequestMapping("/edit")
    @ResponseBody
    public Map<String, Object> edit(int id,String name, String phone) {
        Link link = linkService.getById(id);
        link.setName(name);
        link.setPhone(phone);
        linkService.updateById(link);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "修改成功");
        return map;
    }

}
